import 'dart:io';

import 'package:bxultimate/data/request/base_request.dart';
import 'package:bxultimate/data/request/user_request.dart';
import 'package:bxultimate/data/store/auth_store.dart';
import 'package:bxultimate/domain/auth/login_user.dart';
import 'package:bxultimate/shared/util/util_mk.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:wechat_camera_picker/wechat_camera_picker.dart';

import 'state.dart';

class UserInformationIndexController extends GetxController {
  final UserInformationIndexState state = UserInformationIndexState();

  TextEditingController nickNameController = TextEditingController();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    nickNameController.text = AuthStore.to.profile.nickname!;
    getData();
  }

  getData() async {
    state.userIdentity = await UserRequest.selectUserIdentityInfoByIdcard();
  }

  updateAvatar(context) async {
    showGeneralDialog(
      context: context,
      pageBuilder: (BuildContext buildContext, Animation<double> animation,
          Animation<double> secondaryAnimation) {
        return TDAlertDialog.vertical(
            title: '请选择图片',
            showCloseButton: true,
            buttons: [
              TDDialogButtonOptions(
                  title: '拍摄',
                  action: () async {
                    final AssetEntity? entity =
                        await CameraPicker.pickFromCamera(
                      context,
                    );
                    if (entity != null) {
                      updateAvatarHandler(entity);
                    }
                  },
                  theme: TDButtonTheme.primary),
              TDDialogButtonOptions(
                  title: '从手机相册选择',
                  titleColor: TDTheme.of(context).brandColor7,
                  action: () async {
                    final List<AssetEntity>? result =
                        await AssetPicker.pickAssets(context,
                            pickerConfig: AssetPickerConfig(
                                textDelegate: AssetPickerTextDelegate(),
                                maxAssets: 1,
                                requestType: RequestType.image));
                    if (result != null) {
                      updateAvatarHandler(result[0]);
                    }
                  },
                  theme: TDButtonTheme.light),
            ]);
      },
    );
  }

  updateAvatarHandler(AssetEntity entity) async {
    File? file = await entity.file;
    SmartDialog.showLoading(msg: "上传中...");
    var response =
        await BaseRequest.uploadWithBtype(file!.path, "user", "headImg");
    LoginUser loginUser = AuthStore.to.profile;
    loginUser = loginUser.copyWith(headImgUrl: response.path);
    AuthStore.to.profile = loginUser;

    await UserRequest.updateNickNameOrAvatar({
      "id": AuthStore.to.profile.id,
      "headImgUrl": response.path,
    });
    SmartDialog.dismiss();
    SmartDialog.showNotify(msg: "上传成功", notifyType: NotifyType.success);
    Get.back();
  }

  void updateNickName() {
    showGeneralDialog(
      context: Get.context!,
      pageBuilder: (BuildContext buildContext, Animation<double> animation,
          Animation<double> secondaryAnimation) {
        return TDInputDialog(
          textEditingController: nickNameController,
          title: "请输入您的新昵称",
          hintText: '',
          rightBtn: TDDialogButtonOptions(
            title: '确定',
            action: () async {
              await UserRequest.updateNickNameOrAvatar({
                "id": AuthStore.to.profile.id,
                "nickname": nickNameController.text,
              });
              SmartDialog.showNotify(msg: "昵称修改成功", notifyType: NotifyType.success);
              AuthStore.to.getProfile();
              Get.back();
            },
          ),
        );
      },
    );
  }
}
